/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.db.obj.vis.ui.actions;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.opu.db.obj.vis.domain.AttributeService;
import org.opu.db.obj.vis.domain.ObjectService;
import org.opu.db.obj.vis.domain.domains.AbstractAttributeEntry;
import org.opu.db.obj.vis.domain.domains.AbstractEntity;
import org.opu.db.obj.vis.domain.domains.ObjectEntry;
import org.opu.db.obj.vis.domain.domains.ObjectId;
import org.opu.db.obj.vis.ui.client.MainView;
/**
 *
 * @author yura
 */
public class Delete extends AbstractAction implements PropertyChangeListener {

	private static final long serialVersionUID = 8375641134305793836L;

	private ObjectService objectService;
    private AttributeService attributeService;


    public void setObjectService(ObjectService publicationService) {
        this.objectService = publicationService;
    }

    public void setAttributeService(AttributeService publisherService) {
        this.attributeService = publisherService;
    }

    @Override
    public String getName() {
        return "Delete";
    }

    @Override
    public void runAction(ActionEvent e) throws Exception {
        super.runAction(e);
        Collection<? extends AbstractEntity> beans = mainView.getSelectedBeans();
        if (beans != null && !beans.isEmpty()){
            for (AbstractEntity bean : beans) {
                if (bean instanceof AbstractAttributeEntry){
                    AbstractAttributeEntry attr = (AbstractAttributeEntry) bean;
                    attributeService.remove(attr);

                    ObjectId objectId = attr.getId().getObjectEntry().getId();
                    ObjectEntry get = objectService.get(objectId);
                    ArrayList<AbstractAttributeEntry> attributes = get.getAttributes();
                    mainView.setContent(attributes, AbstractAttributeEntry.class);
                } else if (bean instanceof ObjectEntry){
                    objectService.remove((ObjectEntry) bean);
                    mainView.setContent(objectService.getObjectsMapping(), ObjectEntry.class);
                } else {
                    throw new IllegalArgumentException("Unknown data type: " + bean);
                }
            }
            mainView.update();
        }
    }

	@Override
	public void setMainView(MainView mainView) {
		super.setMainView(mainView);
		mainView.addChangeListener(this);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (MainView.SELECTED_BEANS.equals(evt.getPropertyName())){
			List newValue = (List) evt.getNewValue();
			setEnabled(newValue != null && !newValue.isEmpty());
		}
	}

}
